package com.pingan.haofang.searchcloud.common.configuration;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;

/**
 * AMQP配置
 * 
 * @author LUYI374
 * @date 2017年3月22日
 * @since 1.0.0
 */
@Configuration
@EnableRabbit
@ConditionalOnProperty(name = "amqp.addresses")
public class AmqpConfiguration implements RabbitListenerConfigurer {

    /**
     * 索引手动构建queue
     */
 //   public static final String INDEX_BUILD_EXECUTION_QUEUE = "queue_index_build_execution";

    /**
     * 索引手动构建exchange
     */
 //   public static final String INDEX_BUILD_EXECUTION_EXCHANGE = "exchange_index_build_execution";

    /**
     * 索引增量构建exchange
     */
    public static final String INDEX_BUILD_INC_EXCHANGE = "exchange_index_build_inc";
    
    @Autowired
    private AmqpProperties config;
    
    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
        registrar.setMessageHandlerMethodFactory(handlerMethodFactory());
    }

    @Bean("cachingConnectionFactory")
    public ConnectionFactory cachingConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(config.getAddresses());
        connectionFactory.setUsername(config.getUsername());
        connectionFactory.setPassword(config.getPassword());
        connectionFactory.setVirtualHost(config.getVirtualHost());
        connectionFactory.setChannelCacheSize(10);
        return connectionFactory;
    }

    @Bean
    public AmqpTemplate amqpTemplate(@Qualifier("cachingConnectionFactory") ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        return template;
    }

    @Bean
    public DefaultMessageHandlerMethodFactory handlerMethodFactory() {
        DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
        return factory;
    }

    @Bean("containerFactory")
    public SimpleRabbitListenerContainerFactory
            rabbitListenerContainerFactory(@Qualifier("cachingConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMaxConcurrentConsumers(1);
        factory.setConcurrentConsumers(1);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }

    @Bean("rabbitAdmin")
    public RabbitAdmin rabbitAdmin(@Qualifier("cachingConnectionFactory") ConnectionFactory connectionFactory) {
        RabbitAdmin admin = new RabbitAdmin(connectionFactory);

        /*admin.declareQueue(new Queue(INDEX_BUILD_EXECUTION_QUEUE, true));

        admin.declareExchange(new DirectExchange(INDEX_BUILD_EXECUTION_EXCHANGE, true, false));*/
        admin.declareExchange(new DirectExchange(INDEX_BUILD_INC_EXCHANGE, true, false));

       /* admin.declareBinding(new Binding(INDEX_BUILD_EXECUTION_QUEUE, Binding.DestinationType.QUEUE,
                INDEX_BUILD_EXECUTION_EXCHANGE, "", null));*/
        return admin;
    }
}
